package cn.redcoral.messageplus.data.model.entity;

import cn.messageplus.core.message.Message;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;

import java.sql.Timestamp;

/**
 * 历史消息类
 * 用于封装不同类型的即时通讯消息
 * @author mo
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@TableName("mp_history_message")
public class HistoryMessage {
    private byte version; // 版本
    private String id; // 消息ID
    private short type; // 消息类型
    private String fromId; // 发送者ID
    private short toType; // 接收方类型
    private String toId; // 接收者ID
    private Long sessionId; // 会话ID
    private String jsonMsg; // 消息数据
    private Timestamp createTime; // 创建时间

    public HistoryMessage(Message message) {
        this.version = message.getVersion();
        this.id = message.getId();
        this.type = message.getType();
        this.fromId = message.getFromId();
        this.toType = message.getToType();
        this.toId = message.getToId();
        this.jsonMsg = JSON.toJSONString(message);
        Timestamp createTime = message.getCreateTime();
        if (createTime != null) {
            this.createTime = createTime;
        } else {
            this.createTime = new Timestamp(System.currentTimeMillis());
        }
    }
}
